﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddEdit.aspx.cs" Inherits="CardLogistics.Web.MainPage.Ordering.Ordering_Cardinfo.AddEdit" %>

<%@ Register Src="../../../Controls/WebUserControl_Nav.ascx" TagName="WebUserControl_Nav"
    TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>卡片订货单_新增编辑</title>
    <link href="../../../css/style.css" rel="stylesheet" type="text/css" />
    <link href="../../../css/selPage.css" rel="stylesheet" type="text/css" />
    <script src="../../../js/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
    <script src="../../../js/valReq.js" type="text/javascript"></script>
    <script src="../../../js/jquery-1.3.2.min.js" type="text/javascript"></script>
    <script src="../../../js/jquery.tipswindow.2.0.js" type="text/javascript"></script>
    <script type="text/javascript" language="javascript">
        function valReq(issubmit) {
            //非空校验
            var altStr = valid_isempty('ORDERNO', '订单编号');
            //必须为字母或者数字
            altStr += valid_isnoCh('ORDERNO', '订单编号');
            altStr += valid_isempty('BATCHNO', '批次号');
            altStr += valid_isempty('PConNo', '合同批次号');
            altStr += valid_isempty('ORDERDATA', '订单日期');
            altStr += valid_isempty('GETDATA', '到货日期');
            altStr += valid_isempty('STARTBOXNO', '起始箱号');
            altStr += valid_isempty('ENDBOXNO', '结束箱号');
            altStr += valid_isempty('STARTNO', '起始卡号');
            altStr += valid_isempty('ENDNO', '结束卡号');
            altStr += valid_isempty_select('ddlCardTypeId', '卡片类型');

            //非数字或字母校验
            altStr += valid_isnoCh('ORDERNO', '订单编号');
            altStr += valid_isnoCh('BATCHNO', '批次号');
            altStr += valid_isnoCh('STARTBOXNO', '起始箱号');

            altStr += GetCardInfo();
            altStr += valid_isempty('hf_FormatId', '版面信息');

            if (altStr != "") {
                alert(altStr);
                return false;
            }
            //判断结束箱号和结束卡号是否为0 并且赠品数量输入是否有误
            if (Validate() == "err")
                return false;
            //校验卡片数量
            if (reqNum() == "err")
                return false;
            else {
                if (issubmit == 1) {
                    if (confirm("提交后该信息进入待审批状态不可编辑,确认提交吗?") == true) {
                        return true;
                    }
                    else
                        return false;
                }
                else
                    return true;
            }
        }

        //判断结束箱号和结束卡号是否为0
        function Validate() {
            var boxNum = parseInt(document.getElementById("tdBOX").value);
            var cardNum = parseInt(document.getElementById("tdNO").value);
            var boxEndNo = parseInt(document.getElementById("ENDBOXNO").value);
            var cardEndNo = parseInt(document.getElementById("ENDNO").value);
            if (boxNum == 0) {
                alert("箱数不可为0，请重新输入!");
                return "err";
            }
            if (cardNum == 0) {
                alert("卡数量不可为0，请重新输入!");
                return "err";
            }
            if (boxEndNo == 0) {
                alert("结束箱号不可为0，请重新输入!");
                return "err";
            }
            if (cardEndNo == 0) {
                alert("结束卡号不可为0，请重新输入!");
                return "err";
            }
            //判断赠品数量输入是否有误
            var sparePartsNum = parseInt(document.getElementById("PreMiumsNum").value);
            var spareParts = parseInt(document.getElementById("PreMiums").value);
            if (sparePartsNum > spareParts) {
                alert("赠品数量不能大于合同赠品数量，请重新输入!");
                return "err";
            }
        }

        //判断版面类型是否重复
        function selCardNoInfo(obj) {
            var stxt = obj.options[obj.options.selectedIndex].text; //版面类型
            var txtTmp = "";
            var snum = 0;
            $("select[name=CardType] option:selected").each(function (i, o) {//所有选中的版面类型集合
                if (Trim($(o).text()) != "") {
                    txtTmp += (Trim($(o).text()));
                    if ((Trim($(o).text())) == stxt)
                        snum++;
                }
            });
            if (snum > 1) {
                alert("版面类型不可重复选择！");
                obj.value = "";
            }
        }
        //校验卡片数量
        function reqNum() {
            var dnum = parseInt($("#ConCardTotal").val()); //合同总数
            var SpareParts = parseInt($("#SpareParts").val()); //备品数量
            var cnum = parseInt($("#OredCardTotal").val()); //已定货数量
            var bcnum = GetSumByName("CurrCardTotal"); //本次发放数量
            var ednum = parseInt($("#hf_EditSumCard").val()); //编辑时总数
            var ynum = dnum + SpareParts - cnum + ednum; //本次订单数量=合同总数-已定货数量+编辑的数量
            if (bcnum == 0) {
                alert("订单数量不可为0,请填写订单明细!");
                return "err";
            }
            if (bcnum > ynum) {
                alert("当前订单数量" + bcnum + "不可大于" + ynum + ",请核对!");
                return "err";
            }
            var cardNum = document.getElementById("tdNO").value; //卡片数量
            var spareNum = document.getElementById("SparePartsNum").value; //备品数量
            //卡片数量合计=卡片数量+备品数量
            var cardSum = parseInt(cardNum) + parseInt(spareNum);
            if (bcnum != cardSum) {
                alert("卡片数量合计" + cardSum + "与版面数量" + bcnum + ",不相符,请核对!");
                return "err";
            }
        }
    </script>
    <script type="text/javascript">
        $(document).ready(function () {    //选择合同单
            $("#PConNo").click(function () {
                $.tipsWindow({
                    ___title: "选择合同单",
                    ___content: "iframe:../../SelectPage/SelContract.aspx?type=order",
                    ___width: "1000",
                    ___height: "500",
                    ___drag: "___boxTitle",
                    ___showbg: true
                });

            });

            //版面信息
            $("input[name='CardNumber']").keyup(function () {

                var cNumber = $("input[name=CardNumber]");
                var csum = 0;
                $("select[name=CardType] option:selected").each(function (i, o) {
                    if ($(cNumber[i]).val() != "0" && $(cNumber[i]).val() != "") {
                        csum += parseInt(Trim($(cNumber[i]).val()));
                        if (Trim($(o).text()) == "") {
                            $(o).focus();
                        }
                    }
                });
                document.getElementById("tsum").innerHTML = csum;
                document.getElementById("CurrCardTotal").value = csum;
            });
        })
        //计算箱数
        function xonkeyup() {
            var reg = /[a-zA-Z]/g;
            var rg = /[0-9]/g;
            var boxSum = document.getElementById("tdBOX").value; //箱数
            var code = document.getElementById("STARTBOXNO").value.replace(rg, ""); //获取字母
            var startboxno = document.getElementById("STARTBOXNO").value.replace(reg, ""); //获取数字
            var endboxno = parseInt(startboxno) + parseInt(boxSum) - 1;
            if (boxSum == "" || startboxno == "" || endboxno == NaN)
                document.getElementById("ENDBOXNO").value = 0;
            else
                document.getElementById("ENDBOXNO").value = code + endboxno;
        }

        //备品数量变化，重新计算结束卡号
        function overSum() {
            
            var txtBakNumber = document.getElementById("SparePartsNum").value;  //当前备品数量
            var txtBenginNo = document.getElementById("STARTNO").value;         //起始卡号
            var txtNumber = document.getElementById("tdNO").value;              //卡数量
            if (txtBakNumber == "" || txtBenginNo == "" || txtNumber == "")
                return "err";
            var endNumber = parseInt(txtBenginNo) + parseInt(txtNumber) + parseInt(txtBakNumber) - 1;
            document.getElementById("ENDNO").value = endNumber;
        }

        //计算卡数量
        function conkeyup() {
            var reg = /[a-zA-Z]/g;
            var cardSum = document.getElementById("tdNO").value; //张数
            var startno = document.getElementById("STARTNO").value.replace(reg, "");
            var endno; //结束卡号
            //备品数量=卡数量* (3/1000)
            var spareNum = parseInt(cardSum * 0.003);
            if (spareNum <= 0)
                document.getElementById("SparePartsNum").value = 0;
            else
                document.getElementById("SparePartsNum").value = spareNum;

            //重建计算结束卡号 结束卡号=开始卡号+卡数量  +备品数量-1//+ parseInt(spareNum)
            endno = parseInt(startno) + parseInt(cardSum) + parseInt(spareNum) - 1;

            if (cardSum == "" || startno == "" || endno == NaN || endno == undefined)
                document.getElementById("ENDNO").value = 0;
            else
                if (Trim(startno).length == 8)
                    document.getElementById("ENDNO").value = Pad(endno, 8);
                else
                    document.getElementById("ENDNO").value = 0;
        }

        function GetCardInfo() {
            var Data_value = [];
            var errInfo = ""; //返回错误消息
            var vIndex = 0;
            var cardNum = 0;
            var CardNumber = $("input[name=CardNumber]");
            $("select[name=CardType] option:selected").each(function (i, o) {
                if ($(CardNumber[i]).val() != "0" && $(CardNumber[i]).val() != "") {
                    Data_value[vIndex] = Trim($(o).val()) + "|" + Trim($(CardNumber[i]).val()); //类型ID+数量
                    cardNum += parseInt(Trim($(CardNumber[i]).val()));
                    vIndex++;
                    if (Trim($(o).text()) == "")
                        errInfo += "请给第" + vIndex + "行选择版面类型！\n";
                }
            });
            document.getElementById("CurrCardTotal").value = cardNum;
            $("#hf_FormatId").val(Data_value.join(";"));
            return errInfo;
        }

        function BindDetailInfo() {
            var csum = 0;
            var cNumber = $("input[name=CardNumber]");
            var cType = $("select[name=CardType]");
            var ids = document.getElementById("hf_FormatId").value;
            var str1 = ids.split(";");
            for (var i = 0; i < str1.length; i++) {
                if (str1[i] != "") {
                    $(cNumber[i]).val(str1[i].split("|")[1]);
                    $(cType[i]).val(str1[i].split("|")[0]);
                    csum += parseInt(str1[i].split("|")[1]);
                }
            }
            document.getElementById("tsum").innerHTML = csum;
            document.getElementById("CurrCardTotal").value = csum;
        }
        window.onload = function () { BindDetailInfo(); xonkeyup(); conkeyup(); }

        function CardType() {
            document.getElementById("BtnId").click();
        }

    </script>
</head>
<body>
    <form id="frmOrder" runat="server">
        <input type="hidden" id="hf_FormatId" name="hf_FormatId" runat="server" />
        <input type="hidden" id="ConCardTotal" runat="server" />
        <input type="hidden" id="OredCardTotal" runat="server" />
        <input type="hidden" id="CurrCardTotal" runat="server" />
        <input type="hidden" id="hf_EditSumCard" runat="server" />
        <input type="hidden" id="SpareParts" runat="server" />
        <input type="hidden" id="PreMiums" runat="server" />
        <input type="hidden" id="DeliveryDays" runat="server" />
        <uc1:WebUserControl_Nav ID="WebUserControl_Nav1" runat="server" />
        <%=TitleName %>
        <table class="theFormM" width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td class="titleTxtM">订单编号：
                </td>
                <td class="contentTxtM">
                    <input id="ORDERNO" runat="server" type="text" class="dfinputNoEmp" maxlength="15" />
                </td>
                <td class="titleTxtM">合同批次号：
                </td>
                <td class="contentTxtM">
                    <input id="PConNo" runat="server" readonly="readonly" type="text" class="dfinputImg"
                        maxlength="25" onblur="javascript:CardType() " />
                    <asp:HiddenField ID="hf_ConId" runat="server" />
                    <asp:Button ID="BtnId" runat="server" OnClick="TypeValue_Click" Width="0px" />
                </td>
                <td class="titleTxtM">订单批次号：
                </td>
                <td class="contentTxtM">
                    <input id="BATCHNO" runat="server" type="text" class="dfinputNoEmp" maxlength="25" />
                </td>

            </tr>
            <tr>
                <td class="titleTxtM">订单日期：
                </td>
                <td class="contentTxtM">
                    <input id="ORDERDATA" runat="server" type="text" class="Wdate" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" />
                </td>
                <td class="titleTxtM">到货日期：
                </td>
                <td class="contentTxtM">
                    <input id="GETDATA" runat="server" type="text" class="Wdate" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" />
                </td>
                <td class="titleTxtM">卡片类型：
                </td>
                <td class="contentTxtM">
                    <asp:DropDownList ID="ddlCardTypeId" runat="server" CssClass="dfinputNoEmp">
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td class="titleTxtM">箱数：
                </td>
                <td class="contentTxtM">
                    <input id="tdBOX" runat="server" type="text" class="dfinputNoEmp" maxlength="8" onkeydown="onlyNum();" onkeyup="xonkeyup();" />
                </td>
                <td class="titleTxtM">起始箱号：
                </td>
                <td class="contentTxtM">
                    <input id="STARTBOXNO" runat="server" type="text" class="dfinputNoEmp" maxlength="8"
                        onkeyup="xonkeyup();" />
                </td>
                <td class="titleTxtM">结束箱号：
                </td>
                <td class="contentTxtM">
                    <input id="ENDBOXNO" runat="server" type="text" class="dfinput" maxlength="8" readonly="readonly" />
                </td>
            </tr>
            <tr>
                <td class="titleTxtM">数量(张)：
                </td>
                <td id="Td1" class="contentTxtM" runat="server">
                    <input id="tdNO" runat="server" type="text" class="dfinputNoEmp" maxlength="8" onkeydown="onlyNum();"
                        onkeyup="conkeyup();" />
                </td>
                <td class="titleTxtM">备品数量/张：
                </td>
                <td class="contentTxtM">
                    <input id="SparePartsNum" runat="server" type="text" class="dfinput"  onkeyup="overSum();"
                        maxlength="9" />
                </td>
                <td class="titleTxtM">赠品数量/张：
                </td>
                <td class="contentTxtM">
                    <input id="PreMiumsNum" runat="server" type="text" class="dfinput" onkeydown="onlyNum();" 
                        maxlength="9" />
                </td>

            </tr>
            <tr>
                <td class="titleTxtM">订单负责人：
                </td>
                <td class="contentTxtM">
                    <input id="PIC" runat="server" type="text" class="dfinput" maxlength="25" />
                </td>

                <td class="titleTxtM">起始卡号：
                </td>
                <td class="contentTxtM">
                    <input id="STARTNO" runat="server" type="text" class="dfinputNoEmp" maxlength="8"
                        onkeydown="onlyNum();" onkeyup="conkeyup();" />
                </td>
                <td class="titleTxtM">结束卡号：
                </td>
                <td class="contentTxtM">
                    <input id="ENDNO" runat="server" type="text" class="dfinput" readonly="readonly"
                        maxlength="8" />
                </td>
            </tr>
            <tr>
                <td class="titleTxtM">备注：
                </td>
                <td class="contentTxtM" colspan="5">
                    <input id="REMARK" runat="server" type="text" class="dfinput" maxlength="200" style="width: 94%;" />
                </td>
            </tr>
            <tr>
                <td class="titleTxtM">合同信息：
                </td>
                <td class="contentTxtM" colspan="5">
                    <asp:Label ID="lblConInfo" runat="server" Text="&nbsp;请选择合同！"></asp:Label>
                </td>
            </tr>
            <tr>
                <td colspan="6">
                    <div class="ddetail" style="height: 197px;">
                        <table class="tabledetail" id="tb_Detail">
                            <thead>
                                <tr>
                                    <th width="65px">序号
                                    </th>
                                    <th width="200px">版面类型
                                    </th>
                                    <th width="200px">数量
                                    </th>
                                    <th>&nbsp;
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td width="70px" align="center">1
                                    </td>
                                    <td width="280px">
                                        <select name="CardType" onchange="selCardNoInfo(this)">
                                            <option value="0"></option>
                                            <%foreach (var CardType in CardTypeList)
                                                {%>
                                            <option value="<%=CardType.Key %>">
                                                <%=CardType.Value %></option>
                                            <% }%>
                                        </select>
                                    </td>
                                    <td>
                                        <input name="CardNumber" type="text" value="0" onkeydown="onlyNum();" maxlength="7" />
                                    </td>
                                    <td></td>
                                </tr>
                                <tr>
                                    <td width="70px" align="center">2
                                    </td>
                                    <td>
                                        <select name="CardType" onchange="selCardNoInfo(this)">
                                            <option value="0"></option>
                                            <%foreach (var CardType in CardTypeList)
                                                {%>
                                            <option value="<%=CardType.Key %>">
                                                <%=CardType.Value %></option>
                                            <% }%>
                                        </select>
                                    </td>
                                    <td>
                                        <input name="CardNumber" type="text" value="0" onkeydown="onlyNum();" maxlength="7" />
                                    </td>
                                    <td>&nbsp;
                                    </td>
                                </tr>
                                <tr>
                                    <td width="70px" align="center">3
                                    </td>
                                    <td>
                                        <select name="CardType" onchange="selCardNoInfo(this)">
                                            <option value="0"></option>
                                            <%foreach (var CardType in CardTypeList)
                                                {%>
                                            <option value="<%=CardType.Key %>">
                                                <%=CardType.Value %></option>
                                            <% }%>
                                        </select>
                                    </td>
                                    <td>
                                        <input name="CardNumber" type="text" value="0" onkeydown="onlyNum();" maxlength="7" />
                                    </td>
                                    <td>&nbsp;
                                    </td>
                                </tr>
                                <tr>
                                    <td width="70px" align="center">4
                                    </td>
                                    <td>
                                        <select name="CardType" onchange="selCardNoInfo(this)">
                                            <option value="0"></option>
                                            <%foreach (var CardType in CardTypeList)
                                                {%>
                                            <option value="<%=CardType.Key %>">
                                                <%=CardType.Value %></option>
                                            <% }%>
                                        </select>
                                    </td>
                                    <td>
                                        <input name="CardNumber" type="text" value="0" onkeydown="onlyNum();" maxlength="7" />
                                    </td>
                                    <td>&nbsp;
                                    </td>
                                </tr>
                                <tr>
                                    <td width="70px" align="center">5
                                    </td>
                                    <td>
                                        <select name="CardType" onchange="selCardNoInfo(this)">
                                            <option value="0"></option>
                                            <%foreach (var CardType in CardTypeList)
                                                {%>
                                            <option value="<%=CardType.Key %>">
                                                <%=CardType.Value %></option>
                                            <% }%>
                                        </select>
                                    </td>
                                    <td>
                                        <input name="CardNumber" type="text" value="0" onkeydown="onlyNum();" maxlength="7" />
                                    </td>
                                    <td>&nbsp;
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <table class="tablesum">
                        <tr>
                            <td width="70px"></td>
                            <td width="280px" style="text-align: center;">合计
                            </td>
                            <td width="189px" id="tsum" style="text-align: right">0
                            </td>
                            <td></td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr class="btntr">
                <td colspan="6" align="center">
                    <asp:Button ID="btn_Submit" CssClass="btn" runat="server" Text="保存并提交" OnClientClick="return valReq(1);"
                        OnClick="btn_Submit_Click" />
                    <asp:Button ID="btn_Add" CssClass="btn" runat="server" Text="保存" OnClientClick="return valReq(0);"
                        OnClick="btn_Add_Click" />
                    <input type="button" class="btn" value="返回" onclick="javascript: history.go(-1);" />
                </td>
            </tr>
        </table>
    </form>
</body>
</html>
